package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.VO.PageInfoResult;
import com.student.VO.SnackVO;
import com.student.dto.CommentDto;
import com.student.dto.ResponseDto;
import com.student.mapper.WordsMapper;
import com.student.pojo.Replies;
import com.student.pojo.Taking;
import com.student.pojo.Venues;
import com.student.pojo.Words;
import com.student.service.WordsService;
import com.student.util.PageResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: SupportGroup
 * @BelongsPackage: com.student.service.impl
 * @Author: 小徐
 * @CreateTime: 2025-03-04 09:23
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class WordsServiceImpl implements WordsService {
    @Autowired
    private WordsMapper wordsMapper;

    @Override
    public ResponseDto getAllWord(Words words) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(words.getCurrentPage(), words.getPageSize());
        List<Words> snackGoodsCates = wordsMapper.getAllWord(words);

        PageInfo<Words> cyCardPageInfo = new PageInfo<>(snackGoodsCates);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto addWords(Words words) {

        Integer res = wordsMapper.addWords(words);
        if (res > 0) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto updateWords(Words words) {
        Integer res = wordsMapper.updateWords(words);
        if (res > 0) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteWords(Integer id) {
        Integer res = wordsMapper.deleteWords(id);
        if (res > 0) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addTaking(SnackVO snackVO) {
        List<Words> words = wordsMapper.selectWords(snackVO.getMsg());
        if (words.size() == 0) {
            Integer res = wordsMapper.addTaking(snackVO);
            if (res>0){
                return ResponseDto.success(null);
            }
        }else {
            return new ResponseDto(2,"该评论违规",null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto getComments(Integer id) {
        // 获取艺人底下的所有评论
        List<Taking> takings = wordsMapper.getComments(id);

        if (takings.isEmpty()) {
            return ResponseDto.error("暂无评论");
        }

        // 将评论及其回复转换为 DTO
        List<CommentDto> commentDtos = takings.stream()
                .map(taking -> {
                    CommentDto commentDto = new CommentDto();
                    commentDto.setId(taking.getId());
                    commentDto.setName(taking.getName());
                    commentDto.setMsg(taking.getMsg());
                    commentDto.setUserId(taking.getUserId());
                    commentDto.setArtId(taking.getArtId());

                    // 获取该评论的所有回复
                    List<Replies> replies = wordsMapper.findByCommentId(taking.getId());
                    List<Replies> replyDtos = replies.stream()
                            .map(reply -> {
                                Replies replyDto = new Replies();
                                replyDto.setId(reply.getId());
                                replyDto.setName(reply.getName());
                                replyDto.setCommentId(reply.getCommentId());
                                replyDto.setUserId(reply.getUserId());
                                replyDto.setContent(reply.getContent());
                                replyDto.setCreateTime(reply.getCreateTime());
                                replyDto.setUpdateTime(reply.getUpdateTime());
                                return replyDto;
                            })
                            .collect(Collectors.toList());

                    commentDto.setReplies(replyDtos);
                    return commentDto;
                })
                .collect(Collectors.toList());
        System.out.println(commentDtos);
        return ResponseDto.success(commentDtos);

//        List<Taking> takings = wordsMapper.getComments(id);
//        if (takings.size() > 0) {
//            return ResponseDto.success(takings);
//        }
//        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addReply(SnackVO snackVO) {
        Integer res = wordsMapper.addReply(snackVO);
        if (res > 0) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }
}